﻿/// <reference path="jquery-1.9.1.js" />
/// <reference path="~/_layouts/15/MicrosoftAjax.js" />
/// <reference path="~/_layouts/15/init.js" />
/// <reference path="~/_layouts/15/SP.Core.js" />
/// <reference path="~/_layouts/15/SP.Runtime.js" />
/// <reference path="~/_layouts/15/SP.UI.Dialog.js" />
/// <reference path="~/_layouts/15/SP.js" />

'use strict';

$(document).ready(function () {
    var selectedContentTypeId = $("#ContentTypeList").val();
    if (selectedContentTypeId != undefined && selectedContentTypeId != "") {
        $(".RulesGridViewWrapper").show();
        $("#AddNewRuleButton").show();
    }
    else {
        $(".RulesGridViewWrapper").hide();
        $("#AddNewRuleButton").hide();
    }
    getContentTypeList();
});

var context = SP.ClientContext.get_current();
var siteCollection = context.get_site();

// This function prepares, loads, and then executes a SharePoint query to get the current users information
function getContentTypeList() {
    context.load(siteCollection);
    context.executeQueryAsync(onGetSiteCollectionSuccess, onGetSiteCollectionFailure);
}

// This function is executed if the above call is successful
// It replaces the contents of the 'message' element with the user name
function onGetSiteCollectionSuccess() {
    var siteUrl = siteCollection.get_url();
    $.ajax({
        url: siteUrl + "/_api/web/contentTypes",
        type: "GET",
        contentType: "application/json;odata=verbose",
        headers: {
            "Accept": "application/json;odata=verbose"
        },
        success: onGetContentTypeListSuccess,       
        error: onGetContentTypeListFailure
    });
}


function onGetSiteCollectionFailure(sender, args) {
    alert("failure");
}

function onGetContentTypeListSuccess(data)
{
    var results = data.d.results;
    //alert(results);
    for (var i = 0; i < results.length; i++) {
        var contentTypeName = results[i].Name;
        var contentTypeId = results[i].Id.StringValue
        $("#ContentTypeList").append("<option value='" + contentTypeId + "'>" + contentTypeName + "</option>")
    }
}

function onGetContentTypeListFailure(data, errorCode, errorMessage) {
    alert(errorMessage);
}

function getMetadataColumnList() {
    var selectedContentTypeId = $("#ContentTypeList").val();
    if (selectedContentTypeId != undefined && selectedContentTypeId != ' ') {
        $(".RulesGridViewWrapper").show();
        $("#AddNewRuleButton").show();
        $.ajax({
            url: siteUrl + "/_api/web/contentTypes('" + selectedContentTypeId + "')/Fields",
            type: "GET",
            contentType: "application/json;odata=verbose",
            headers: {
                "Accept": "application/json;odata=verbose"
            },
            success: onGetMetadataColumnListSuccess,
            error: onGetMetadataColumnListFail
        });        
    }
}

function onGetMetadataColumnListSuccess(data) {
    $("#MetadataColumnList").find("option:gt(0)").remove();    
    var results = data.d.results;
    for (var i = 0; i < results.length; i++) {
        var type = results[i].TypeAsString;
        if (type == 'Text' || type == 'Boolean' || type == 'Number') {
            var columnId = results[i].Id;
            var columnDisplayName = results[i].Title;
            $("#MetadataColumnList").append("<option value='" + columnId + "'>" + columnDisplayName + "</option>")
        }
    }
}

function onGetMetadataColumnListFail(data, errorCode, errorMessage) {
    displayErrorMessage(errorMessage);
    alert(errorMessage);
}


